package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.query.DestinationQuery;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("/destination")
public class DestinationController {

    @Autowired
    private IDestinationService destinationService;




    // 目的地显示
    // 这里需要的是条件分页的数据
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") DestinationQuery qo){
        Page<Destination> page = destinationService.query(qo);

        // 导航吐司
        List<Destination> toasts = destinationService.gettoasts(qo.getParentId());

        model.addAttribute("toasts",toasts);
        model.addAttribute("page",page);
        return "/destination/list";
    }


    /**
     * 编辑修改简介
     * @param id
     * @param info
     * @return
     */
    @RequestMapping("/updateInfo")
    @ResponseBody
    public JsonResult updateInfo(String id,String info){
        // 根据传上来的id找到目的地对象
        Destination destination = destinationService.get(id);
        // 把传上来的简介 String Info set 回对象的Info属性中
        destination.setInfo(info);

        // 使用update方法
        destinationService.update(destination);

        return JsonResult.success();
    }











}
